﻿using System;
using System.Web.Caching;

namespace MVCFoto.BLL
{
    /// <summary>
    /// Cache Help class
    /// </summary>
    internal class CacheHelper
    {
        public delegate TT GetCacheItemDelegate<TT>();
        /// <summary>
        /// 取得缓存的对象
        /// </summary>
        /// <typeparam name="TT">对象类型</typeparam>
        /// <param name="cacheKey">缓存key</param>
        /// <param name="syncLock">同步锁</param>
        /// <param name="priority">优先级</param>
        /// <param name="refreshAction">刷新时间,一般为null</param>
        /// <param name="cacheDependency">缓存依赖项</param>
        /// <param name="timeout">缓存时间</param>
        /// <param name="getCacheItem">对象没被缓存时获取对象的方法委托</param>
        /// <returns></returns>
        public static TT GetCacheItem<TT>(string cacheKey, object syncLock,
            CacheItemPriority priority, CacheItemRemovedCallback refreshAction,
            CacheDependency cacheDependency, TimeSpan timeout, GetCacheItemDelegate<TT> getCacheItem)
        {
            object result = System.Web.HttpRuntime.Cache.Get(cacheKey);
            if (result == null)
            {
                lock (syncLock)
                {
                    result = System.Web.HttpRuntime.Cache.Get(cacheKey);
                    if (result == null)
                    {
                        result = getCacheItem();
                        if(result!=null)
                            System.Web.HttpRuntime.Cache.Add(cacheKey, result, cacheDependency,
                                DateTime.Now.Add(timeout), TimeSpan.Zero, priority, refreshAction);
                    }
                    
                }
            }
            return (TT)result;
        }

        /// <summary>
        /// 取得缓存的对象
        /// </summary>
        /// <typeparam name="TT">对象类型</typeparam>
        /// <param name="cacheKey">缓存key</param>
        /// <param name="syncLock">同步锁</param>
        /// <param name="priority">优先级</param>
        /// <param name="refreshAction">刷新时间,一般为null</param>
        /// <param name="cacheDependency">缓存依赖项</param>
        /// <param name="timeout">缓存时间</param>
        /// <param name="getCacheItem">对象没被缓存时获取对象的方法委托</param>
        /// <returns></returns>
        public static TT GetCacheItem<TT>(string cacheKey, object syncLock,
            TimeSpan timeout, GetCacheItemDelegate<TT> getCacheItem)
        {
            return GetCacheItem<TT>(cacheKey, syncLock, CacheItemPriority.Normal, null, null, timeout, getCacheItem);
        }
        /// <summary>
        /// 缓存对象
        /// </summary>
        /// <typeparam name="TT"></typeparam>
        /// <param name="cacheKey"></param>
        /// <param name="syncLock"></param>
        /// <param name="cacheDependencyFileName">缓存依赖的文件</param>
        /// <param name="getCacheItem"></param>
        /// <returns></returns>
        public static TT GetCacheItem<TT>(string cacheKey, object syncLock, string cacheDependencyFileName,
            GetCacheItemDelegate<TT> getCacheItem)
        {
            return GetCacheItem<TT>(cacheKey,
                syncLock,
                CacheItemPriority.Normal,
                null,
                new CacheDependency(cacheDependencyFileName),
                TimeSpan.FromHours(1),
                getCacheItem);
        }


    }
}
